/*
   Copyright (c) 2016-2017 Slamtec Co., Ltd. All Rights Reserved.
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
       http://www.apache.org/licenses/LICENSE-2.0
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package com.csjbot.snowbot_rogue.servers.slams.agent;


import com.csjbot.csjbase.log.Csjlogger;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ExecutorService;

class Worker_back {
    private final ArrayList<Runnable> jobQueue = new ArrayList<>();


    private final ArrayList<Runnable> jobQueue2 = new ArrayList<>();
    private final Thread mExecThread;

    private ExecutorService workerExecutor;


    private final Object WorkerLock = new Object();
    private Thread worker;
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            ArrayList<Runnable> jobs = new ArrayList<>();
            Csjlogger.info("Work start");
            while (true) {
                synchronized (jobQueue) {
                    jobs.addAll(jobQueue);
                    jobQueue.clear();
                }

                if (jobs.isEmpty()) {
                    break;
                }

                for (Runnable job : jobs) {
                    // Logger.d("chenqi " + job);
                    Csjlogger.info("job is {}", job);
                    job.run();
                }
                jobs.clear();
            }

            synchronized (StateLock) {
                state = IDLE;
            }
        }
    };

    private final static int IDLE = 0;
    private final static int WORKING = 1;

    private int state;
    private final Object StateLock = new Object();

    Worker_back() {
        state = IDLE;
        mExecThread = new Thread(new Runnable() {
            @Override
            public void run() {
                executeRunnable();
            }
        }, "UART_Manager_Write_Thread");
        mExecThread.setDaemon(true);
        mExecThread.start();
    }

    private void executeRunnable() {
        Csjlogger.info("==== WORKER_EXECUTOR run");
        while (true) {
            synchronized (jobQueue2) {
                Iterator<Runnable> jobIterator = jobQueue2.iterator();
                while (jobIterator.hasNext()) {
                    Runnable job = jobIterator.next();
                    Csjlogger.info("job is {} , run", job);

                    job.run();
                    jobIterator.remove();
                }

                try {
                    Csjlogger.info("jobQueue2  wait");
                    jobQueue2.wait();
                } catch (InterruptedException e) {
                    //ignore
                }
            }
        }
    }

    void push(Runnable job) {
        synchronized (jobQueue2) {
            Csjlogger.info("push job {} ", job);
            jobQueue2.add(job);
            jobQueue2.notify();
        }
//            synchronized (jobQueue) {
//            jobQueue.add(job);
//        }
//
//        execute();
    }

    void pushHead(Runnable job) {
        synchronized (jobQueue2) {
            Csjlogger.info("pushHead job {} ", job);
            jobQueue2.add(0, job);
            jobQueue2.notify();
        }

//        synchronized (jobQueue) {
//            jobQueue.add(0, job);
//        }
//
//        execute();
    }

    void clear() {
        synchronized (jobQueue2) {
            jobQueue2.clear();
        }
//        synchronized (jobQueue) {
//            jobQueue.clear();
//        }
    }

    private void execute() {
        synchronized (StateLock) {
            if (state == WORKING) {
                Csjlogger.warn("state is  WORKING return  ");
                return;
            }
            state = WORKING;
        }

        if (worker.getState() == Thread.State.TERMINATED) {
//            worker = new Thread(runnable);
            Csjlogger.warn("worker new ");
        }

        if (worker.getState() == Thread.State.WAITING) {
            synchronized (WorkerLock) {
                worker.notify();
                Csjlogger.warn("worker notify ");
            }
        }

        if (worker.getState() == Thread.State.NEW) {
            worker.start();
            Csjlogger.warn("worker start ");
        }
    }
}
